using System;
using Csla;
using Ent.AccesoDatos;
using System.Collections.Generic;
using System.Data;

namespace EntGuayasSLA.BL
{
    [Serializable]
    public class Informe : BusinessBase<Informe>
    {
        #region Business Methods
        
        public static readonly PropertyInfo<Guid> IdProperty = RegisterProperty<Guid>(c => c.Id);
        public Guid Id
        {
            get { return GetProperty(IdProperty); }
            set { SetProperty(IdProperty, value); }
        }
        
        public static readonly PropertyInfo<string> EstadoProperty = RegisterProperty<string>(c => c.Estado);
        public string Estado
        {
            get { return GetProperty(EstadoProperty); }
            set { SetProperty(EstadoProperty, value); }
        }
        
        public static readonly PropertyInfo<string> NotificaProperty = RegisterProperty<string>(c => c.Notifica);
        public string Notifica
        {
            get { return GetProperty(NotificaProperty); }
            set { SetProperty(NotificaProperty, value); }
        }

        public static readonly PropertyInfo<string> TipoProperty = RegisterProperty<string>(c => c.Tipo);
        public string Tipo
        {
            get { return GetProperty(TipoProperty); }
            set { SetProperty(TipoProperty, value); }
        }

        public static readonly PropertyInfo<string> PersonaIdProperty = RegisterProperty<string>(c => c.PersonaId);
        public string PersonaId
        {
            get { return GetProperty(PersonaIdProperty); }
            set { SetProperty(PersonaIdProperty, value); }
        }

        public static readonly PropertyInfo<string> TablaIdProperty = RegisterProperty<string>(c => c.TablaId);
        public string TablaId
        {
            get { return GetProperty(TablaIdProperty); }
            set { SetProperty(TablaIdProperty, value); }
        }

        private string _Fk { get; set; }
        #endregion

        #region Business Rules

        protected override void AddBusinessRules()
        {
            // TODO: add validation rules
            base.AddBusinessRules();

            //BusinessRules.AddRule(new Rule(IdProperty));
        }

        private static void AddObjectAuthorizationRules()
        {
            // TODO: add authorization rules
            //BusinessRules.AddRule(...);
        }

        #endregion
        [Serializable()]
        private class CriteriaPrivateInforme
        {

            private string _id;
            public string Id { get { return _id; } }
            private string _fk;
            public string Fk { get { return _fk; } }
            public CriteriaPrivateInforme(string id, string fk)
            {
                _id = id;
                _fk = fk;

            }

        }
        #region Factory Methods

        public static Informe NewEditableRoot(string fk)
        {
            return DataPortal.Create<Informe>(fk);
        }

        public static Informe GetEditableRoot(Guid id)
        {
            return DataPortal.Fetch<Informe>( id);
        }

        public static void DeleteEditableRoot(Guid id)
        {
            DataPortal.Delete<Informe>(id);
        }

        private Informe()
        { /* Require use of factory methods */ }

        #endregion
       

        #region Data Access

        [RunLocal]
        protected override void DataPortal_Create()
        {
            // TODO: load default values
            // omit this override if you have no defaults to set
            base.DataPortal_Create();
        }
        private void DataPortal_Create(string _fk)
        {
            
            this._Fk = _fk;
            base.DataPortal_Create();

        }
        private void DataPortal_Fetch(Guid criteria)
        {
            
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
                new Parametro("ec_inf_id", criteria.ToString())
            };
         
            DataSet datos = accesoDatos.EjecutarDataSet("\"OBT_INFORME\"", parametros);
            if (datos.Tables[0].Rows.Count > 0)
            {
                /*
                 * inf_id                               fal_id                               mon_id     cpm_id     fac_id     arc_id                               per_id                               otr_id     inf_fecha  inf_estado inf_notifica inf_tipo   inf_per_notifica 
                   7f0a5ead-300a-4773-98ab-2596b33cb15d 83882203-0947-4925-94bd-70ec0b065c5e null       null       null       7f0a5ead-300a-4773-98ab-2596b33cb15d d890080d-a67d-4e30-ae52-5c10f762eb31 null       04/12/2013 SUBIDO     N            INFDOCLIC  null             
                 */
                LoadProperty(IdProperty, Guid.Parse(datos.Tables[0].Rows[0]["inf_id"].ToString()));
                LoadProperty(EstadoProperty, datos.Tables[0].Rows[0]["inf_estado"].ToString());
                LoadProperty(NotificaProperty, datos.Tables[0].Rows[0]["inf_notifica"].ToString());
                LoadProperty(TipoProperty, datos.Tables[0].Rows[0]["inf_tipo"].ToString());
                LoadProperty(PersonaIdProperty, datos.Tables[0].Rows[0]["per_id"].ToString());
                switch (ReadProperty(TipoProperty))
                {
                    case Valores.Modulo_Archivo_Informes_Licenciamiento:
                        LoadProperty(TablaIdProperty, datos.Tables[0].Rows[0]["fal_id"].ToString());
                        break;
                    case Valores.Modulo_Archivo_Informes_Aud_Cump:
                        LoadProperty(TablaIdProperty, datos.Tables[0].Rows[0]["fac_id"].ToString());
                        break;
                    case Valores.Modulo_Archivo_Informes_Aud_Moni:
                        LoadProperty(TablaIdProperty, datos.Tables[0].Rows[0]["mon_id"].ToString());
                        break;
                    case Valores.Modulo_Archivo_Informes_Aud_Pma:
                        LoadProperty(TablaIdProperty, datos.Tables[0].Rows[0]["cpm_id"].ToString());
                        break;
                    case Valores.Modulo_Archivo_Informes_Aud_Otro:
                        LoadProperty(TablaIdProperty, datos.Tables[0].Rows[0]["otr_id"].ToString());
                        break;
                }
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
                new Parametro("ec_inf_id", ReadProperty(IdProperty).ToString()),
                new Parametro("ec_per_id", ReadProperty(PersonaIdProperty)),
                new Parametro("ec_inf_estado", ReadProperty(EstadoProperty)),
                new Parametro("ec_inf_notifica", ReadProperty(NotificaProperty)),
                new Parametro("ec_fal_id", ReadProperty(TablaIdProperty)),
                new Parametro("ec_tabla", ReadProperty(TipoProperty))
            };
            accesoDatos.EjecutarQuery("\"ACT_INFORME\"", parametros);
            SendEmailNotificacion.Promotor(ReadProperty(NotificaProperty), null, ReadProperty(IdProperty).ToString(), ReadProperty(TablaIdProperty), ReadProperty(TipoProperty), "ACT_INFORME");
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new CriteriaPrivateInforme(this.Id.ToString(), this._Fk));
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Delete(CriteriaPrivateInforme criteria)
        {
          
        }

        #endregion
    }
}
